#!/bin/sh
WORK_DIR=`pwd`

export COMPANY=JiaYuan
export APP_NAME=`grep APP_NAME ./release.txt |awk -F ': ' '{print $2}'|sed 's/[[:space:]]//g'`
export APP_VERSION=`grep APP_VERSION ./release.txt |awk -F ': ' '{print $2}'|sed 's/[[:space:]]//g'`

export GLOBE_CONF=application.yml

export NOW_DATE=`date "+%Y%m%d"`

export LOG_FOLDER=${WORK_DIR}/log
mkdir -p ${LOG_FOLDER}
#export APP_LOG=$(cd "$(dirname $0)";pwd)/log/build-$APP_NAME-$APP_VERSION-${NOW_DATE}.log
export APP_LOG=${LOG_FOLDER}/build-$APP_NAME-$APP_VERSION-${NOW_DATE}.log

echo "WORK_DIR: "$WORK_DIR

LogMsg()
{
	msg_data=$*
	now_time=`date "+%Y-%m-%d %T"`
        if [ -z "$msg_data" ];then
                msg_data=""
        fi
	echo "\e[0m ${now_time} INFO : $msg_data \e[0m"
	echo "\e[0m ${now_time} INFO : $msg_data \e[0m" >> $APP_LOG
}
LogSuc()
{
	msg_data=$*
	now_time=`date "+%Y-%m-%d %T"`
	if [ -z "$msg_data" ];then
		msg_data=""
	fi
	echo "\e[32m ${now_time} SUCCESS : $msg_data \e[0m"
	echo "\e[32m ${now_time} SUCCESS : $msg_data \e[0m" >> $APP_LOG
}
LogWarn()
{
        msg_data=$*
	now_time=`date "+%Y-%m-%d %T"`
        if [ -z "$msg_data" ];then
                msg_data=""
        fi
	echo "\e[33m ${now_time} WARN : $msg_data \e[0m"
	echo "\e[33m ${now_time} WARN : $msg_data \e[0m" >> $APP_LOG
}
LogErr()
{
        msg_data=$*
	now_time=`date "+%Y-%m-%d %T"`
        if [ -z "$msg_data" ];then
                msg_data=""
        fi
	echo "\e[41m ${now_time} ERROR : $msg_data \e[0m"
	echo "\e[41m ${now_time} ERROR : $msg_data \e[0m" >> $APP_LOG
}

ShowHelp()
{
        LogWarn "Input error ! You must input zero params !"
        LogMsg " Build the App use: sh build.sh"
        exit 1
}

if [ $# -ne 0 ]; then
        ShowHelp
        exit 1
fi

LogMsg "APP_NAME: "$APP_NAME
LogMsg "APP_VERSION: "$APP_VERSION
LogMsg "APP_LOG: "$APP_LOG

compile_app(){

LogMsg "start compile app"
cd ${WORK_DIR}
mvn clean

cd ${WORK_DIR}/flink-function
mvn clean compile

cd ${WORK_DIR}/flink-datasource
mvn clean install

cd ${WORK_DIR}/carbon-access-app
mvn clean install

cd ${WORK_DIR}
mvn clean install

mvn dependency:copy-dependencies

}

build_app(){
  LogMsg "start build app"

  cd ${WORK_DIR}

  export APP_INSTALL_NAME=$APP_NAME-$APP_VERSION-${NOW_DATE}
  export APP_INSTALL_FOLDER=${WORK_DIR}/target/$APP_INSTALL_NAME

  rm -fr  $APP_INSTALL_FOLDER
  mkdir -p $APP_INSTALL_FOLDER
  LogMsg "Build to folder:"${APP_INSTALL_FOLDER}

# cp -f  ${WORK_DIR}/helm-storage-data/target/helm-storage-data-0.0.1-SNAPSHOT.jar $APP_INSTALL_FOLDER
# cp -fr ${WORK_DIR}/helm-storage-data/target/dependency  $APP_INSTALL_FOLDER
  cp -f  ${WORK_DIR}/carbon-access-app/target/carbon-access-app-0.0.1-SNAPSHOT.jar $APP_INSTALL_FOLDER
  cp -fr ${WORK_DIR}/carbon-access-app/target/dependency  $APP_INSTALL_FOLDER
  cp -fr ${WORK_DIR}/target/dependency  $APP_INSTALL_FOLDER/plugin
  cp -f  ${WORK_DIR}/carbon-access-app/src/main/resources/$GLOBE_CONF $APP_INSTALL_FOLDER
  cp -f  ${WORK_DIR}/release.txt $APP_INSTALL_FOLDER
  cp -f  ${WORK_DIR}/script/start.sh $APP_INSTALL_FOLDER
  cp -f  ${WORK_DIR}/script/stop.sh $APP_INSTALL_FOLDER
  cp -f  ${WORK_DIR}/script/restart.sh $APP_INSTALL_FOLDER
  cp -f  ${WORK_DIR}/script/flinksee.sh $APP_INSTALL_FOLDER
  cp -f  ${WORK_DIR}/script/mission-by-flink.service $APP_INSTALL_FOLDER
  cp -f  ${WORK_DIR}/script/install.sh $APP_INSTALL_FOLDER
  cp -f  ${WORK_DIR}/script/kill.sh $APP_INSTALL_FOLDER
  cp -f  ${WORK_DIR}/script/h2Shell.sh $APP_INSTALL_FOLDER

  cd ${WORK_DIR}/target
  tar -zcvf $APP_NAME-$APP_VERSION-${NOW_DATE}.tar.gz $APP_INSTALL_NAME
  #rm -fr  $APP_INSTALL_FOLDER
  cd ${WORK_DIR}

  echo "ls ${APP_INSTALL_FOLDER}"
  ls ${APP_INSTALL_FOLDER}

  cd ${WORD_DIR}
  LogSuc "app build finish"
  LogMsg "Build app package in  folder : [ ${WORK_DIR}/target ]"
  LogMsg "Look app package use command : ll ${WORK_DIR}/target"

}

git pull 

compile_app

build_app

